<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>查看成绩</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f0f2f5;
            margin: 0;
            padding: 20px;
        }
        h1 {
            text-align: center;
            color: #007bff;
        }
        .search-container {
            display: flex;
            margin: 10px 0;
        }
        .search-container input[type="text"] {
            flex: 1;
            padding: 10px;
            font-size: 16px;
            box-sizing: border-box;
        }
        .search-container button {
            padding: 10px;
            background: linear-gradient(to right, #007bff, #290b65);
            color: white;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        .search-container button:hover {
            background:linear-gradient(to right, #007bff, #290b65);
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
        th {
            background-color: #007bff;
            color: white;
        }
    </style>
</head>
<body>
    <h1>查看成绩</h1>
    <div class="search-container">
        <input type="text" id="searchInput" placeholder="输入关键字进行搜索...">
        <button onclick="search()">查询</button>
    </div>
    <table id="resultTable">
        <thead>
            <tr>
                <th>学号</th>
                <th>课程编号</th>
                <th>姓名</th>
                <th>课程名</th>
                <th>分数</th>
            </tr>
        </thead>
        <tbody>
            <!-- 查询结果将插入到这里 -->
        </tbody>
    </table>

    <script>
        function search() {
            let query = document.getElementById('searchInput').value.trim();
            
            // 如果输入框中为 *，则将查询字符串置为空，相当于查询全部
            if (query === '*') {
                query = '';
            }

            fetch(`/search-grades?query=${encodeURIComponent(query)}`)
                .then(response => response.json())
                .then(data => {
                    const tableBody = document.getElementById('resultTable').querySelector('tbody');
                    tableBody.innerHTML = ''; // 清空表格内容
                    data.forEach(row => {
                        const tr = document.createElement('tr');
                        Object.values(row).forEach(text => {
                            const td = document.createElement('td');
                            td.textContent = text;
                            tr.appendChild(td);
                        });
                        tableBody.appendChild(tr);
                    });
                })
                .catch(error => console.error('Error fetching table data:', error));
        }
    </script>
</body>
</html>
